package com.oraycn.esbasic.utils;

import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class ViewUtil {

    public interface IDrawableListener{
        void onRightDrawableTouch(); 
    }

    public static void setOnTouchRightDrawableListener(final TextView textView, final IDrawableListener listener) {
        if(listener==null){return;}
        textView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP)
                {
                    int i=0;
                }


                // et.getCompoundDrawables()得到一个长度为4的数组，分别表示左右上下四张图片
                Drawable drawable = textView.getCompoundDrawables()[2];
                //如果右边没有图片，不再处理
                if (drawable == null)
                    return false;
                //如果不是按下事件，不再处理
                if (event.getAction() != MotionEvent.ACTION_DOWN)
                    return false;
                if (event.getX() > textView.getWidth()
                        - textView.getPaddingRight()
                        - drawable.getIntrinsicWidth()) {
                    //隐藏软键盘
                    v.setFocusableInTouchMode(false);
                    v.setFocusable(false);
                    //do something
                    listener.onRightDrawableTouch();
                } else {
                    v.setFocusableInTouchMode(true);
                    v.setFocusable(true);
                    return false;
                }
                return true;
            }
        });

    }
}

